package com.zte.warehouse.control;


import com.zte.sys.login.model.LUsers;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import com.zte.warehouse.model.*;
import com.zte.warehouse.service.IInWarehouseService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 仓库管理模块
 * <p>
 * 仓库管理模块使用者：财务部门经理  job_id=6
 * 创建仓库权限：财务部门经理
 */


@Controller
@RequestMapping("/InWarehouse")
public class InWarehouseAction {
    @Autowired
    private IInWarehouseService InWarehouseService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/InInWarehousePage")
    @ResponseBody
    @OperatorLog(operate="多条件查询入库分页", module="入库管理")
    public ResultMessage getInInWarehousesPage(PageModel<InWarehose> pageModel, InWarehouseCondition conn) {

        //1.身份验证：判断用户jobID, 财务部门经理看全部,非财务部门经理看自己
        LUsers User = (LUsers) redisTemplate.opsForValue().get(conn.getUserID() + "");
        ResultMessage msg = new ResultMessage();
        if (User != null) {
            if (User.getJobId() == 6l||User.getJobId() == 1l) {//财务部门经理、总经理
                conn.setUserID(null);
//                System.out.println("JobId:"+User.getJobId());
            }
            System.out.println("NowUser:" + User);
        }
        PageModel<InWarehose> inWarehosePageModel = InWarehouseService.queryInWarehoseListByCondition(pageModel, conn);
        msg.setStatus(200);
        msg.setMsg("查询成功！");
        msg.setData(inWarehosePageModel);

        return msg;
    }


    //添加入库信息
    @RequestMapping("/InWarehouseAdd")
    @ResponseBody
    @OperatorLog(operate="添加入库信息，更新采购表入库状态", module="入库管理")
    public ResultMessage addInWarehouse(AddInWarehose addInWarehose) {
        System.out.println("addInWarehose"+addInWarehose);
        addInWarehose.setState("1");//默认未入库

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date creatTime = sdf.parse(addInWarehose.getInTime());
        int i = InWarehouseService.addInWarehose(addInWarehose);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("添加入库成功！");
        msg.setData(i);
        return msg;
    }

    //test
//    @RequestMapping("/test")
//    @ResponseBody
//    public ResultMessage test(InWarehouseUpdateCondition con) {
//        System.out.println(con);
//
//        Purchase purchase = InWarehouseService.testFunction(con);
//
//        ResultMessage msg = new ResultMessage();
//        msg.setStatus(200);
//        msg.setMsg("test！");
//        msg.setData(purchase);
//        return msg;
//    }
    //更新入库状态
    @RequestMapping("/updateInWarehouseState")
    @ResponseBody
    @OperatorLog(operate="更新入库状态，并更新仓库金额、库存商品", module="入库管理")
    public ResultMessage updateInWarehouseState(InWarehouseUpdateCondition con) {
        System.out.println(con);
        int i = InWarehouseService.updateInWarehose(con);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("更新入库状态成功！");
        msg.setData(i);
        return msg;
    }

    //验证采购单审核状态
    @RequestMapping("/queryPurchaseState")
    @ResponseBody
    public ResultMessage queryPurchaseState(String indent) {

        Purchase purchase = InWarehouseService.queryPurchaseState(indent);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("查询状态成功！");
        msg.setData(purchase);
        return msg;
    }


}
